package cn.gson.fengboot.controller;

import cn.gson.fengboot.aspect.MyLog;
import cn.gson.fengboot.model.pojos.*;
import cn.gson.fengboot.model.service.DbusinessService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController//表示这个是一个回答控制器，能直接返回类，处理成Json
@RequestMapping("/business")
public class DbusinessController {

    @Autowired
    DbusinessService service;

    /*查询(商机列表查询，模糊查询，分页，商机进度查询)方法*/

    @RequestMapping("/mybusiness")
    public Map<String,Object> selectmybusiness(Integer currentPage, Integer pagesize,String busName, String phaseName,HttpServletRequest request){
        Page<Object> obj = PageHelper.startPage(currentPage, pagesize);
        List<Dbusiness> selectbusiness = service.selectmybusiness(busName,phaseName,request);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        map.put("buslist",service.selectmybusiness(busName,phaseName,request));
        return map;
    }

    @RequestMapping("/allbusiness")
    public Map<String,Object> selectallbusiness(Integer currentPage, Integer pagesize,String busName, String phaseName,HttpServletRequest request){

        Page<Object> obj = PageHelper.startPage(currentPage, pagesize);

        List<Dbusiness> selectbusiness = service.selectallbusiness(busName,phaseName,request);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        map.put("buslist",service.selectallbusiness(busName,phaseName,request));
        return map;
    }

    /*郑雄峰查询(商机列表查询，模糊查询，分页，商机进度查询)方法*/
    @RequestMapping("/kehushangji")
    public Map<String,Object> kehu(Integer page, Integer size,String search,String search2){
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Dbusiness> selectbusiness = service.kehushangji(search,search2);
        Map<String,Object> map = new HashMap<>();

        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        //map.put("total",info.getTotal());
        //map.put("rows",info.getList());
        return map;
    }

    /*查询商机进度方法*/
    @RequestMapping("/phase")
    public List<Dphase> selectphase(){
        return service.selectphase();
    }


    /*新增商机*/
    @RequestMapping("/addbusiness")
    @MyLog(modul = "售前模块",desc = "新增商机")
    public Boolean addbusiness(String busfrom){
        JSONObject obj = (JSONObject)JSONObject.parse(busfrom);
        Dbusiness dbusiness=obj.getObject("busfrom",Dbusiness.class);
        List budIdlist = obj.getObject("dbuspros", List.class);

        return service.addbusiness(dbusiness,budIdlist);
    }

    /*从登录session获取员工姓名*/
    @RequestMapping("/getbuspeople")
    public String getbuspeople(HttpServletRequest request){
        QempFrom emp=(QempFrom)request.getSession().getAttribute("user");
        String busPeople = null;
        if(emp!=null){
            busPeople = emp.getEmpName();
        }
        //取session
        return busPeople;
    };

    /*从登录session获取员工ID*/
    @RequestMapping("/getbuspeopleid")
    public Integer getbuspeopleId(HttpServletRequest request){
        QempFrom emp=(QempFrom)request.getSession().getAttribute("user");
        Integer empId =null;
        if(emp!=null){
            empId = emp.getEmpId();
        }
        //取session
        return empId;
    };


    /*根据客户查询联系人*/
    @RequestMapping("/selectmainlinkman")
    public List<TfylinkmanForm> selectmainlinkman(String search,String linkmanName){
        return service.selectmainlinkman(search,linkmanName);

    }

    /*根据产品ID查询产品信息*/
    @RequestMapping("/selectprodetails")
    public Map<String,Object> allpro(Integer currentpage, Integer pagesize,
                                     String proName){
        Page<Object> obj = PageHelper.startPage(currentpage, pagesize);
        Map<String,Object> map = new HashMap<>();

        service.allpro(proName);

        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

    /*根据ID查询商机详情*/
    @RequestMapping("/selectBybusId")
    public Dbusiness selectBybusId(Integer busId){
        return service.selectBybusId(busId);
    }

    /*删除商机产品*/
    @RequestMapping("/detproById")
    @MyLog(modul = "售前模块",desc = "删除商机产品")
    public void detproById(Integer budproId){
        service.detproById(budproId);

    }

    /*删除商机（修改商机状态）*/
    @RequestMapping("/detbus")
    @MyLog(modul = "售前模块",desc = "删除商机")
    public Boolean detbus(String budId){
        return service.detbus(budId);
    }


    /*修改商机联系人*/
    @RequestMapping("/editlinkman")
    @MyLog(modul = "售前模块",desc = "修改商机联系人")
    public void editlinkman(Integer busId,Integer linkmanId){
        service.editlinkman(busId,linkmanId);
    }


    @RequestMapping("/allclientbyemp")
    public Map<String,Object> allclientbyemp(String search,Integer currentpage, Integer pagesize,HttpServletRequest request){
        Page<Object> obj = PageHelper.startPage(currentpage, pagesize);
        Integer busPeople=getbuspeopleId(request);//取session存的是登录ID
        service.allclientbyemp(search, busPeople);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",obj.getResult());
        return map;
    }

}
